const mysql = require('mysql2/promise');
const bcrypt = require('bcrypt');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: '123456', // 使用您的实际密码
  database: 'chatime',
  port: 3306
};

async function createTestData() {
  const pool = mysql.createPool(dbConfig);
  
  try {
    console.log('🔗 连接数据库...');
    await pool.getConnection();
    console.log('✅ 数据库连接成功');
    
    // 清空现有数据
    console.log('🗑️ 清空现有数据...');
    await pool.execute('DELETE FROM dynamic_comments');
    await pool.execute('DELETE FROM dynamic_likes');
    await pool.execute('DELETE FROM dynamics');
    await pool.execute('DELETE FROM chat_messages');
    await pool.execute('DELETE FROM friends');
    await pool.execute('DELETE FROM users');
    
    // 创建测试用户
    console.log('👥 创建测试用户...');
    const testUsers = [
      {
        id: 'user1',
        phone: '13800138001',
        password: await bcrypt.hash('123456', 10),
        name: '李文静',
        avatar: 'avatar1',
        signature: '茶香袅袅，岁月静好',
        following: 156,
        followers: 234,
        dynamics: 67
      },
      {
        id: 'user2',
        phone: '13800138002',
        password: await bcrypt.hash('123456', 10),
        name: '王明远',
        avatar: 'avatar2',
        signature: '紫砂壶泡普洱，人生一大乐事',
        following: 89,
        followers: 156,
        dynamics: 34
      },
      {
        id: 'user3',
        phone: '13800138003',
        password: await bcrypt.hash('123456', 10),
        name: '陈雨桐',
        avatar: 'avatar3',
        signature: '茶道即人道，品茶品人生',
        following: 203,
        followers: 445,
        dynamics: 123
      }
    ];
    
    for (const user of testUsers) {
      await pool.execute(`
        INSERT INTO users (id, phone, password, name, avatar, signature, following, followers, dynamics, create_time, update_time) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
      `, [
        user.id,
        user.phone,
        user.password,
        user.name,
        user.avatar,
        user.signature,
        user.following,
        user.followers,
        user.dynamics
      ]);
      console.log(`✅ 创建用户: ${user.name}`);
    }
    
    // 创建好友关系
    console.log('🤝 创建好友关系...');
    const friendships = [
      { id: 'f1', user_id: 'user1', friend_id: 'user2' },
      { id: 'f2', user_id: 'user1', friend_id: 'user3' },
      { id: 'f3', user_id: 'user2', friend_id: 'user1' },
      { id: 'f4', user_id: 'user2', friend_id: 'user3' },
      { id: 'f5', user_id: 'user3', friend_id: 'user1' },
      { id: 'f6', user_id: 'user3', friend_id: 'user2' }
    ];
    
    for (const friendship of friendships) {
      await pool.execute(`
        INSERT INTO friends (id, user_id, friend_id, add_time) 
        VALUES (?, ?, ?, NOW())
      `, [friendship.id, friendship.user_id, friendship.friend_id]);
    }
    console.log('✅ 好友关系创建完成');
    
    // 创建聊天消息
    console.log('💬 创建聊天消息...');
    const chatMessages = [
      {
        id: 'msg1',
        sender_id: 'user1',
        receiver_id: 'user2',
        content: '你好，最近怎么样？',
        type: 'text',
        timestamp: new Date(Date.now() - 3600000) // 1小时前
      },
      {
        id: 'msg2',
        sender_id: 'user2',
        receiver_id: 'user1',
        content: '挺好的，你呢？',
        type: 'text',
        timestamp: new Date(Date.now() - 3500000)
      },
      {
        id: 'msg3',
        sender_id: 'user1',
        receiver_id: 'user2',
        content: '我也不错，明天一起喝茶吧',
        type: 'text',
        timestamp: new Date(Date.now() - 3400000)
      },
      {
        id: 'msg4',
        sender_id: 'user1',
        receiver_id: 'user3',
        content: '茶具收到了，谢谢！',
        type: 'text',
        timestamp: new Date(Date.now() - 7200000) // 2小时前
      },
      {
        id: 'msg5',
        sender_id: 'user3',
        receiver_id: 'user1',
        content: '不客气，喜欢就好！',
        type: 'text',
        timestamp: new Date(Date.now() - 7100000)
      }
    ];
    
    for (const message of chatMessages) {
      await pool.execute(`
        INSERT INTO chat_messages (id, sender_id, receiver_id, content, type, timestamp, is_read) 
        VALUES (?, ?, ?, ?, ?, ?, ?)
      `, [
        message.id,
        message.sender_id,
        message.receiver_id,
        message.content,
        message.type,
        message.timestamp,
        true
      ]);
    }
    console.log('✅ 聊天消息创建完成');
    
    // 创建动态
    console.log('📝 创建动态...');
    const dynamics = [
      {
        id: 'd1',
        user_id: 'user1',
        content: '今日得一款陈年普洱，汤色红浓明亮，香气陈香馥郁，入口醇厚回甘。与三五好友共品，谈古论今，甚是惬意。',
        like_count: 42,
        comment_count: 8,
        create_time: new Date(Date.now() - 86400000) // 1天前
      },
      {
        id: 'd2',
        user_id: 'user2',
        content: '周末和家人一起品茗，感受温馨时光。茶香袅袅，岁月静好。',
        like_count: 35,
        comment_count: 5,
        create_time: new Date(Date.now() - 172800000) // 2天前
      },
      {
        id: 'd3',
        user_id: 'user3',
        content: '新入手一套紫砂壶，泡茶更添风味。欢迎茶友们来品鉴！',
        like_count: 27,
        comment_count: 3,
        create_time: new Date(Date.now() - 259200000) // 3天前
      }
    ];
    
    for (const dynamic of dynamics) {
      await pool.execute(`
        INSERT INTO dynamics (id, user_id, content, like_count, comment_count, create_time, update_time) 
        VALUES (?, ?, ?, ?, ?, ?, ?)
      `, [
        dynamic.id,
        dynamic.user_id,
        dynamic.content,
        dynamic.like_count,
        dynamic.comment_count,
        dynamic.create_time,
        dynamic.create_time
      ]);
    }
    console.log('✅ 动态创建完成');
    
    // 创建评论
    console.log('💭 创建评论...');
    const comments = [
      { id: 'c1', dynamic_id: 'd1', user_id: 'user2', content: '此茶我也曾品过，确是上品。不知何处购得？' },
      { id: 'c2', dynamic_id: 'd1', user_id: 'user3', content: '好茶配好友，真是人生一大乐事！' },
      { id: 'c3', dynamic_id: 'd1', user_id: 'user1', content: '下次一起喝茶！' },
      { id: 'c4', dynamic_id: 'd2', user_id: 'user1', content: '家庭聚会最温暖！' },
      { id: 'c5', dynamic_id: 'd2', user_id: 'user3', content: '茶具很有韵味~' },
      { id: 'c6', dynamic_id: 'd3', user_id: 'user1', content: '紫砂壶泡普洱最合适！' },
      { id: 'c7', dynamic_id: 'd3', user_id: 'user2', content: '下次带点好茶来！' }
    ];
    
    for (const comment of comments) {
      await pool.execute(`
        INSERT INTO dynamic_comments (id, dynamic_id, user_id, content, create_time) 
        VALUES (?, ?, ?, ?, NOW())
      `, [
        comment.id,
        comment.dynamic_id,
        comment.user_id,
        comment.content
      ]);
    }
    console.log('✅ 评论创建完成');
    
    console.log('\n🎉 测试数据创建完成！');
    console.log('\n📱 登录信息：');
    console.log('手机号: 13800138001, 密码: 123456 (李文静)');
    console.log('手机号: 13800138002, 密码: 123456 (王明远)');
    console.log('手机号: 13800138003, 密码: 123456 (陈雨桐)');
    console.log('\n💬 聊天数据：用户1和用户2、用户1和用户3之间有聊天记录');
    console.log('🤝 好友关系：三个用户互为好友');
    console.log('📝 动态数据：每个用户都有发布的动态和评论');
    
  } catch (error) {
    console.error('❌ 创建测试数据失败:', error);
  } finally {
    await pool.end();
  }
}

// 运行脚本
createTestData(); 